<?php
/**
 * 监视器 监控端口号运行状态
 */

require_once __DIR__ . '/../variant/start.php';
\Seasx\SeasLogger\Logger::setLogger('monitor');

$emsPort = \app\common\model\Config::get('ems_server.port', 'ems');
$smsPort = \app\common\model\Config::get('sms_server.port', 'sms');
$imPort = \app\common\model\Config::get('im_server.port', 'im');

/**
 * 需要检查的端口号
 */
$ports = [$emsPort, $smsPort, $imPort];

/**
 *计时器 每隔10秒检查端口状态
 */
swoole_timer_tick(10000, function ($timerId) use ($ports) {
    foreach ($ports as $value) {
        $command = "netstat -anp 2>/dev/null | grep {$value} | grep LISTEN | wc -l";
        $result = shell_exec($command);
        if ($result > 0) {
            $log = $value . ': is running!';
        } else {
            $log = $value . ': not open!';
        }
        $info = [
            '监控端口日志',
            '|',
            $log
        ];
        $logger = \variant\library\vari\Log::instance();
        $logger->notice(implode(' ', $info) . PHP_EOL);
    }

});